#include "ETurtle.h"
#include "D3DApp.h"
#include "Collision.h"
#include "ObPlayer.h"
#include "ObBullet.h"
#include "ObBrick.h"
#include "ObBrickSP.h"
#include "ObDeathField.h"

IDirect3DTexture9* ETurtle::mTex	= 0;
CSound* ETurtle::sDie				= 0;
ETurtle::ETurtle(){
	setId(RUAXANH);
	setInfo(LV1_LEFT);
}

ETurtle::ETurtle(ENERMY1 nid, RUAINFO ninfo){
	mID = nid;
	setInfo(ninfo);
}

ETurtle::~ETurtle(){

}

bool ETurtle::setId(int nid){
	if(checkType(nid)){
		mID = nid;
		mWidth	= 16.0f;
		mHeight = 24.0f;
		MAX_VELOCITYX	= DEFAULTSPEED_X*4;
		MAX_VELOCITYY	= 0;
		
		mVelocity		= D3DXVECTOR3(-DEFAULTSPEED_X,0.0f,0.0f);
		mIsYGravity = true;
		mIsXGravity = false;

		mIsTurnAround	= true;
		mCurrentSprite	= 0;
		mCurrentAction	= RUN;

		count		= 0.0f;
		delaycount	= 0.0f;
		return true;
	}else{
		return false;
	}
}

bool ETurtle::setInfo(int ninfo){
	if(checkInfo(ninfo)){
		mInfo = ninfo;
		if(ninfo == LV1_RIGHT || ninfo == LV2_RIGHT){
			mRightFaced = RIGHT;
			mVelocity.x = DEFAULTSPEED_X;
		}else{
			mRightFaced = LEFT;
			mVelocity.x = -DEFAULTSPEED_X;
		}

		if(ninfo == LV1_RIGHT || ninfo == LV1_LEFT){
			mLevel = 1;
		}else{
			mLevel = 2;
		}

		if(ninfo == LV0){
			mVelocity.x = 0.0f;
			mLevel = 0;
		}
		setStatus(mLevel);
		return true;
	}else{
		return false;
	}
}

bool ETurtle::checkInfo(int ninfo){
	if(ninfo<LV1_LEFT || ninfo > LV0){
		return false;
	}
	return true;
}

int ETurtle::getLevel(){
	return mLevel;
}

bool ETurtle::checkType(int nid){
	if(nid<RUAXANH || nid>RUADO){
		return false;
	}
	return true;
}

void ETurtle::kill(bool permakill, bool mustdeath){
	
	if(permakill && mLevel == 0){
		delaycount = 2.0f;
		PlaySound(sDie);
		mCurrentAction = DIE;
		mNeedCollision = false;
		mVelocity.x = 0.0f;
		mVelocity.y = 300.0f;
		mIsYGravity = true;
		return;
	}

	if(mustdeath){
		delaycount = 2.0f;
		PlaySound(sDie);
		mCurrentAction = DIE;
		mNeedCollision = false;
		mVelocity.x = 0.0f;
		mVelocity.y = 300.0f;
		mIsYGravity = true;
		return;
	}

	if(mLevel > 0){
		mLevel--;
		setStatus(mLevel);
	}

	if(mLevel == 0){
		mCurrentAction = DONOTHING;
		mVelocity.x = 0.0f;
		mVelocity.y = 0.0f;
		
	}
}

void ETurtle::collidedWith(ObObject* object, CollisionRS rs){
	switch (object->getId())
	{
	case ObPlayer::MARIO:
		if(ObPlayer* player  = dynamic_cast<ObPlayer*>(object)){
			if(player->getIsGoD()){
				kill(true,true);
				return;
			}
			if(rs.mState == Top){
				if(mLevel == 0){
					if(mVelocity.x == 0.0f){
						if(player->getPos().x<mPos.x){
							mVelocity.x = DEFAULTSPEED_X*4;
						}else{
							mVelocity.x = -DEFAULTSPEED_X*4;
						}
					}else{
						mVelocity.x = 0.0f;
					}
					return;
				}
				kill(false,false);
			}
			
		}
		break;
	case ObBullet::BL1:
		if(ObBullet* player  = dynamic_cast<ObBullet*>(object)){
			kill(true,true);
		}
		break;

	case ObDeathField::Deathfield1:
		mNeedDispose = true;
		break;
	default:
		if(rs.mState == Left){
			mVelocity.x = -mVelocity.x;
			if(mVelocity.x < 0){
				mRightFaced = LEFT;
			}else{
				mRightFaced = RIGHT;
			}
		}

		if(rs.mState == Right){
			mVelocity.x = -mVelocity.x;
			if(mVelocity.x < 0){
				mRightFaced = LEFT;
			}else{
				mRightFaced = RIGHT;
			}
		}

		if(rs.mState == Bottom){
			mVelocity.y = 0.0f;
			mIsJump = false;
		}

		if(ObBrick* brick  = dynamic_cast<ObBrick*>(object)){
			if(brick->getCanKill()){
				kill(true,true);
			}
		}else if(ObBrickSP* bricksp  = dynamic_cast<ObBrickSP*>(object)){
			if(bricksp->getCanKill()){
				kill(true,true);
			}
		}
		break;
	}
}

void ETurtle::update(float dt){
	if(mNeedDispose) return;

	//sprite count
	if(mCurrentAction == DIE){
		delaycount-=dt;
		mSpritePos.x = 10;
		mSpritePos.y = 50;
		mCurrentSprite = 0;
		if(delaycount <= 0.0f){
			mNeedDispose = true;
			delaycount = 0.0f;
		}
		if(mID == RUADO){
			mSpritePos.x +=80.0f;
		}
		return;
	}

	if(mLevel == 2 && mIsJump == false){
		if(delaycount==0.0f){
			delaycount = 0.3f;
			mIsJump	= true;
		}
	}

	if(mIsJump == true && delaycount > 0.0f){
		delaycount-=dt;
		mVelocity.y = 1200.0f;
		if(delaycount <= 0.0f){
			delaycount = 0.0f;
			mVelocity.y = 0.0f;
		}
	}

	count+=dt;
	if(count >=0.2f){
		if(mCurrentAction == DONOTHING){
			mSpritePos.x = 10.0f;
			mSpritePos.y = 50.0f;
		}else{
			mCurrentSprite++;
			if(mLevel == 0){
				if(mCurrentSprite >=4) mCurrentSprite = 0;
				mSpritePos.y = 50.0f;
				mSpritePos.x = 10.0f + mCurrentSprite*20.0f;
			}else if(mLevel == 1){
				if(mCurrentSprite >=2) mCurrentSprite = 0;
				mSpritePos.y = 20.0f;
				mSpritePos.x = 10.0f + mCurrentSprite*20.0f;
			}else{
				if(mCurrentSprite >=2) mCurrentSprite = 0;
				mSpritePos.y = 20.0f;
				mSpritePos.x = 10.0f + (mCurrentSprite + 2)*20.0f;
			}
		}
		if(mID == RUADO){
			mSpritePos.x +=80.0f;
		}
		count = 0.0f;
	}
}

void ETurtle::render(ID3DXSprite* mSprite){
	if(mID == 0) return;
	if(mTex == 0) return;
	if(mCurrentSprite == -1) return;
	if(mNeedDispose) return;

	HR(gDevice->SetRenderState(D3DRS_ALPHATESTENABLE,true));
	D3DXMATRIX S;
	if(mVelocity.x<=0){
		D3DXMatrixScaling(&S,1.0f,-1.0f,1.0f);
	}else{
		D3DXMatrixScaling(&S,-1.0f,-1.0f,1.0f);
	}
	D3DXMATRIX R;
	D3DXMatrixRotationZ(&R, mRotation);
	D3DXMATRIX T;
	D3DXMatrixTranslation(&T,mPos.x - gCamera->getPos().x, mPos.y - gCamera->getPos().y, 0);
	mSprite->SetTransform(&(S*R*T));
	RECT rect = calRenderRECT(mSpritePos,mWidth,mHeight);
	mSprite->Draw(mTex,&rect,&mCenter,0,D3DCOLOR_XRGB(255, 255, 255));
	mSprite->Flush();
	HR(gDevice->SetRenderState(D3DRS_ALPHATESTENABLE,false));
}

void ETurtle::setStatus(int lv){
	mLevel = lv;
	switch (lv)
	{
	case 0:
		mWidth	= 16.0f;
		mHeight = 16.0f;
		mInfo = LV0;
		return;
	case 1:
		mWidth	= 16.0f;
		mHeight = 24.0f;
		mInfo = (mRightFaced == RIGHT)?LV1_RIGHT:LV1_LEFT;
		break;
	case 2:
		mWidth	= 16.0f;
		mHeight = 24.0f;
		mInfo = (mRightFaced == RIGHT)?LV2_RIGHT:LV2_LEFT;
		break;
	default:
		mWidth	= 16.0f;
		mHeight = 24.0f;
		mInfo = (mRightFaced == RIGHT)?LV1_RIGHT:LV1_LEFT;
		break;
	}
	mCenter = D3DXVECTOR3(mWidth/2,mHeight/2,0.0f);
}

bool ETurtle::loadTexture(){
	sDie = LoadSound("Sounds\\EnemyDie.wav");
	if(mTex == 0){
		D3DXCreateTextureFromFileEx(gDevice,"Sprites/Turtle.png",
			D3DX_DEFAULT_NONPOW2,
			D3DX_DEFAULT_NONPOW2,
			0,
			0,
			D3DFMT_UNKNOWN,
			D3DPOOL_MANAGED,
			D3DX_DEFAULT,
			D3DX_DEFAULT,
			D3DCOLOR_XRGB(0,0,0),
			NULL,
			NULL,
			&mTex);
	}
	return true;
}

bool ETurtle::releaseTexture(){
	if(sDie != 0){
		delete sDie;
	}

	if(mTex != 0){
		ReleaseCOM(mTex);
	}
	return true;
}
